<template>
  <!-- 支付面板 -->
  <van-action-sheet
    :show="show"
    @update:show="emits('update:show', $event)"
    title="选择支付方式"
    :closeable="false"
    :close-on-click-overlay="false"
    @click-overlay="onClose"
  >
    <div class="pay-type">
      <p class="amount">￥{{ actualPayment }}</p>
      <van-cell-group>
        <van-radio-group v-model="payMode">
          <van-cell title="微信支付">
            <template #icon
              ><icon class="cp-icon" name="consult-wechat"
            /></template>
            <template #extra>
              <van-radio name="0"></van-radio>
            </template>
          </van-cell>
          <van-cell title="支付宝支付">
            <template #icon
              ><icon class="cp-icon" name="consult-alipay"
            /></template>
            <template #extra>
              <van-radio name="1"></van-radio>
            </template>
          </van-cell>
        </van-radio-group>
      </van-cell-group>
      <div class="btn">
        <van-button
          type="primary"
          round
          block
          :disabled="payMode === ''"
          @click="pay"
          >立即支付</van-button
        >
      </div>
    </div>
  </van-action-sheet>
</template>

<script setup lang="ts" name="pay-sheet">
import { ref, watch, watchEffect } from 'vue'
import { Toast } from 'vant'
import request from '@/utils/request'
import { ApiRes } from '@/types/data'
const props = defineProps<{
  orderId: string
  show: boolean
  actualPayment: number
  onClose?: () => void
  payCallback: string
}>()
const { orderId, payCallback, actualPayment, onClose } = props
const emits = defineEmits<{
  (e: 'update:show', val: boolean): void
}>()
const payMode = ref('')
// 订单id
let _orderId = orderId
watch(
  () => props.orderId,
  () => {
    _orderId = props.orderId
  }
)
// watchEffect(() => {
//   console.log('---orderId111---', props.orderId)
//   _orderId = props.orderId
// })

// 支付
const pay = async () => {
  if (payMode.value === '0') {
    Toast({ message: '微信支付当前不支持!!!', duration: 1000 })

    return
  }

  const res = await request.post<ApiRes<{ payUrl: string }>>(
    'patient/consult/pay',
    {
      paymentMethod: payMode.value,
      orderId: _orderId,
      payCallback: import.meta.env.VITE_CALLBACK + payCallback
    }
  )

  if (res.data.code === 10000) {
    window.location.href = res.data.data.payUrl
  }
}
</script>
<style lang="scss" scoped>
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
